<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/service/customer' }">客户管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加客户</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 添加数据的表单 -->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="名称" prop="user">
        <el-input v-model="ruleForm.user"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="科室" prop="departmentName">
        <template>
          <el-select v-model="ruleForm.departmentId" placeholder="请选择">
            <el-option
                v-for="item in departmentData"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="预约时间" prop="appointment">
        <template>
          <el-date-picker
              v-model="date"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
          </el-date-picker>
          <el-time-select
              v-model="time"
              :picker-options="{start:'08:30',step:'00:15',end: '21:30'}"
              placeholder="选择时间">
          </el-time-select>
        </template>
      </el-form-item>
      <el-form-item label="病史" prop="patient_history">
        <el-input v-model="ruleForm.patient_history"></el-input>
      </el-form-item>
      <el-form-item label="病情描述" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="住址" prop="address">
        <el-input v-model="ruleForm.address"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input v-model="ruleForm.state"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="gotoList">返回列表</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      date: '2023-06-01',
      time: '08:30',
      //科室选择表
      departmentData: [],
      // 表单
      ruleForm: {
        user: '测试客户名',
        phone:'测试电话',
        departmentName: '',
        departmentId:'',
        appointment: '',
        patient_history: '无',
        description: '无',
        address: '高老庄',
        state:'正常',
      },
      // 表单规则
      rules: {
        user: [
          {required: true, message: '请输入客户名称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入客户电话', trigger: 'blur'},
          {min: 4, max: 25, message: '长度在 4 到 25 个字符', trigger: 'blur'}
        ],
        departmentName: [
          {required: true, message: '请输入科室', trigger: 'blur'},
          {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
        ],
        patient_history: [
          {required: true, message: '请输入患病历史', trigger: 'blur'},
          {min: 1, max: 255, message: '长度在 1 到 255 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入病情描述', trigger: 'blur'},
          {min: 1, max: 255, message: '长度在 1 到 255 个字符', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入客户住址', trigger: 'blur'},
          {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
        ],
        state: [
          {required: true, message: '请输入客户状态', trigger: 'blur'},
          {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    // 返回列表
    gotoList() {
      this.$router.push('./');
    },
    // 提交表单

    submitForm(formName) {
      console.log(this.ruleForm.departmentId);
      // console.log(this.date);
      // console.log(this.time);
      for (let i = 0; i < this.departmentData.length; i++) {
        if (this.departmentData[i].id === this.ruleForm.departmentId) {
          this.ruleForm.departmentName = this.departmentData[i].name;
          break;
        }
      }
      console.log(this.ruleForm.departmentId);

      if(!(this.date===null)&&!(this.time===null)){
        this.ruleForm.appointment = this.date +' '+this.time+':00';
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9999/customers/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
          this.axios
              .create({'headers': {'Authorization': loginInfo.token}})
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(jsonResult.message);
            }
          });
        } else {
          alert('error submit!!');
          return false;
        }
      });
    },
    loadDepartmentList() {
      let url = 'http://localhost:9999/departments?page=1&queryType='+'all';
      console.log('url = ' + url);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .get(url).then((response) => {
        let jsonResult = response.data;

        if (jsonResult.state == 20000) {
          this.departmentData = jsonResult.data.list;
        }
      });
    },

    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadDepartmentList();

  },
}
</script>

<style scoped>
</style>